<?php
class Arisan extends CI_Controller{
private $limit = 10;
    function  __construct() {
        parent::__construct();
        $this->load->model('Arisan_model');
		$this->load->helper(array('form', 'url'));
		$this->load->library(array('form_validation', 'pagination'));
      }

	function index($offset=''){
	
	   $this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('arisan/index/');
		$config['total_rows'] = $this->Arisan_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Arisan_model->countData();
		$data['arisan']	= $this->Arisan_model->get_list_data($this->limit,$offset);

        $this->load->view('admin_views/arisan/arisan', $data);
    }

    function a_risan()
	{
		$this->load->view('admin_views/arisan/a_risan');
	}
      function lookup(){
		
        $keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Arisan_model->selectBarangArisan($keyword); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        'value1' => $row->intcicilan,
                                        'value2' => $row->intuang_muka,
                                        ''
                                     ); 
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 

        }
        else
        {
            $this->load->view('admin_views/a_risan/index',$data); 
        }
	}

    
function lookupBarang(){
		
        $keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Arisan_model->selectBarang($keyword); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        ''
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
 function lookupPeserta(){
		
        $keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Arisan_model->selectPeserta($keyword); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_dealer,
                                        'value' => $row->strnama_dealer,
                                        ''
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 

        }
        else
        {
            $this->load->view('admin_views/a_risan/index',$data); 
        }
	}

    
    function detail($intid_arisan){
		$profile = $this->Arisan_model->selectDetail($intid_arisan);

			foreach($profile as $m)
			{
				$data['strnama']		= $m->strnama;
				$data['strwilayah']		= $m->strwilayah;
				$data['intharga_arisan']     	= $m->intharga_arisan;
				$data['intuang_muka']          	= $m->intuang_muka;
				$data['intuang_cicilan']       	= $m->intuang_cicilan;
				$data['intcicilan']		= $m->intcicilan;
			}
		//$data['privilege'] = $this->privilege;
		$this->load->view('admin_views/arisan/detail', $data);
	}

 //end test
	function add()
	{
		$this->form_validation->set_rules('intid_barang', 'Nama Barang', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_wilayah', 'Nama Wilayah', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intharga_arisan', 'Harga Arisan', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intuang_muka', 'Uang Muka', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intcicilan', 'Cicilan', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intuang_cicilan', 'Uang Cicilan', 'trim|required|xss_clean');

		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');

		if ($this->form_validation->run() == FALSE)
		{
			$wilayah = $this->Arisan_model->selectWilayah();
                       foreach ($wilayah as $g)

			{
				$data['idw'][]	 	= $g->intid_wilayah;
				$data['namaw'][] 	= $g->strwilayah;
			}

			$this->load->view('admin_views/arisan/add', $data);

                      
		}else{

            $this->Arisan_model->insert($_POST);
			redirect('arisan/arisan');
        }
	}
	function SaveDetailArisan()
	{
		$this->form_validation->set_rules('intid_barang', 'Nama Barang', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intuang_muka', 'Uang Muka', 'trim|required|xss_clean');
		

		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');

		if ($this->form_validation->run() == FALSE)
		 {
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
                       $this->load->view('admin_views/arisan/a_risan');
		}else{

                        $this->Arisan_model->insert($_POST);
			redirect('arisan/a_risan');
                }
	}

      function edit($intid_arisan)
		{
			if($_POST==NULL)
			{
                            $wilayah = $this->Cabang_model->selectWilayah();
                       foreach ($wilayah as $g)

			{
				$data['idw'][]	 	= $g->intid_wilayah;
				$data['namaw'][] 	= $g->strwilayah;
			}


				$dataarisan = $this->Arisan_model->select($intid_arisan);
				foreach ($dataarisan as $g)
				{
					$data['intid_arisan']	 	= $g->intid_arisan;
					$data['intid_barang'] 		= $g->intid_barang;
					$data['intid_wilayah']   	= $g->intid_wilayah;
					$data['intharga_arisan'] 	= $g->intharga_arisan;
					$data['intuang_muka'] 		= $g->intuang_muka;
					$data['intuang_cicilan']	= $g->intuang_cicilan;
					$data['intcicilan'] 		= $g->intcicilan;
					
				}
				$this->load->view('admin_views/arisan/edit', $data);
			}else {
				$this->Arisan_model->update($intid_arisan);
				redirect('arisan/index');
			}

		}


    function delete($intid_arisan)
	{
		$this->Arisan_model->delete($intid_arisan);
		redirect('arisan/arisan');
	}
function search()
        {
    $q= $this->input->post('term');
    $data['response'] = 'false';

    $query_arisan=$this->Arisan_model->getSearch($q);
    $arisan = $query_arisan->result();
    if(!empty ($arisan)){

        $data['response']=TRUE;
        $data['message'][]=array('label'=>$row->intuang_muka,'value'=>$row->intid_arisan);
    }
    echo json_encode($data);
        }
    function lookupDetail(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Arisan_model->selectBarang($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
        function insertdetail($intid_darisan)
        {

 }
}
?>
